<template>
  <!-- 注册内容 -->
  <div class="register">
    <h3>
      注册新用户
      <span class="go">我有账号，去 <router-link to="/login">登陆</router-link> </span>
    </h3>
    <ValidationObserver ref="form">
      <!-- form表单 -->
      <form @submit.prevent="submitForm">
        <div class="content">
          <!-- 3、使用组件 -->
          <!-- rules规则名字 -->
          <!-- slot错误信息 -->

          <!-- 必填、长度为11、手机号格式 -->
          <ValidationProvider name="phone" rules="required|length:11|phone" v-slot="{ errors }" ref="phone">
            <label>手机号:</label>
            <input type="text" placeholder="请输入你的手机号" v-model.trim="formInput.phone" />
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
        </div>
        <div class="content">
          <!-- 必填、六位、纯数字 -->
          <ValidationProvider name="getCode" rules="required|digits:6" v-slot="{ errors }">
            <label>验证码:</label>
            <input type="text" placeholder="请输入验证码" v-model.trim="formInput.getCode" />
            <button type="button" class="getCode" @click="getSendCode">获取验证码</button>
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
        </div>
        <div class="content">
          <!-- 必填、长度大于8 -->
          <ValidationProvider name="password" rules="required|min:6|max:12|alpha_num" v-slot="{ errors }">
            <label>登录密码:</label>
            <input type="password" placeholder="请输入你的登录密码" v-model.trim="formInput.password" />
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
        </div>
        <div class="content">
          <!-- 必填、和密码内容一致 -->
          <ValidationProvider name="surePassword" :rules="{ required: true, is: formInput.password }" v-slot="{ errors }">
            <label>确认密码:</label>
            <input type="password" placeholder="请输入确认密码" v-model.trim="formInput.surePassword" />
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
        </div>
        <div class="controls">
          <ValidationProvider name="agreement" rules="required|agree" v-slot="{ errors }">
            <input name="m1" type="checkbox" v-model.trim="formInput.agree" />
            <span>同意协议并注册《智品汇用户协议》</span>
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
        </div>
        <div class="btn">
          <button>完成注册</button>
        </div>
      </form>
    </ValidationObserver>
  </div>
</template>

<script>
// 1、引入包
// 验证单个规则，验证form表单中全部规则
import { ValidationProvider, ValidationObserver } from 'vee-validate'
// // 4、自定义认证规则
// extend('max', {
//   // 验证规则
//   validate: (value, { length }) => {
//     return value.length === Number(length)
//   },
//   // 自定义规则
//   params: ['length'],
//   // 提示信息
//   message: '手机号不符合{length}位',
// })
export default {
  name: 'Register',
  data() {
    return {
      formInput: {
        phone: '',
        getCode: '',
        password: '',
        surePassword: '',
        agree: true,
      },
    }
  },
  // 2、注册组件
  components: {
    ValidationProvider,
    ValidationObserver,
  },
  methods: {
    async submitForm() {
      // 验证表单 是个异步操作
      const success = await this.$refs.form.validate()
      if (!success) return
      // console.log('验证成功！')
      try {
        await this.$store.dispatch('register/postUserRegister', { phone: this.formInput.phone, password: this.formInput.password, code: this.formInput.getCode })
        this.$router.push('/login')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取验证码
    async getSendCode() {
      const { valid } = await this.$refs.phone.validate()
      if (!valid) return alert('手机号输入不正确！')
      const sendCode = await this.$store.dispatch('register/getSendCode', this.formInput.phone)
      this.formInput.getCode = sendCode
    },
  },
}
</script>

<style lang="scss" scoped>
.register {
  width: 1200px;
  height: 445px;
  border: 1px solid rgb(223, 223, 223);
  margin: 0 auto;

  h3 {
    background: #ececec;
    margin: 0;
    padding: 6px 15px;
    color: #333;
    border-bottom: 1px solid #dfdfdf;
    font-size: 20.04px;
    line-height: 30.06px;

    span {
      font-size: 14px;
      float: right;

      a {
        color: #e1251b;
      }
    }
  }

  div:nth-of-type(1) {
    margin-top: 40px;
  }

  .content {
    padding-left: 390px;
    margin-bottom: 18px;
    position: relative;

    label {
      font-size: 14px;
      width: 96px;
      text-align: right;
      display: inline-block;
    }

    input {
      width: 270px;
      height: 38px;
      padding-left: 8px;
      box-sizing: border-box;
      margin-left: 5px;
      outline: none;
      border: 1px solid #999;
    }

    img {
      vertical-align: sub;
    }

    .error-msg {
      position: absolute;
      top: 100%;
      left: 495px;
      color: red;
    }
    button {
      width: 100px;
      height: 38px;
      margin-left: 12px;
    }
  }

  .controls {
    text-align: center;
    position: relative;

    input {
      vertical-align: middle;
    }

    .error-msg {
      position: absolute;
      top: 100%;
      left: 495px;
      color: red;
    }
  }

  .btn {
    text-align: center;
    line-height: 36px;
    margin: 17px 0 0 55px;

    button {
      outline: none;
      width: 270px;
      height: 36px;
      background: #e1251b;
      color: #fff !important;
      display: inline-block;
      font-size: 16px;
    }
  }
}
</style>
